import configparser
import sys

import command
import config
import constants


def ssl_cmd(command_line):
    ssl_arg = ('-Dmaven.wagon.http.ssl.insecure=true '
               '-Dmaven.wagon.http.ssl.allowall=true '
               '-Dmaven.wagon.http.ssl.ignore.validity.dates=true')
    command_line += (' ' + ssl_arg)
    return command_line


def skip_tests(command_line):
    command_line += ' -DskipTests'
    return command_line


def offline_cmd(command_line):
    command_line += ' --offline'
    return command_line


def get_command_line():
    get_config = config.get_config()
    return get_config.get(constants.MAVEN, constants.CMD)


def switch_maven_cmd(cmd):
    get_config = config.get_config()
    get_config.set(constants.MAVEN, constants.CMD, cmd)
    config.save_config(get_config)


def set_jdk(get_config: configparser.ConfigParser = None):
    if get_config is None:
        get_config = config.get_config()

    jdk_config = get_config.options(constants.JDK)
    if jdk_config is not None:
        set_jdk_continue = True
        while set_jdk_continue:
            for index, jdk in enumerate(jdk_config):
                print(index + 1, jdk.strip())
            user_input = int(input("Please select jdk version: "))
            if user_input <= len(jdk_config):
                set_jdk_continue = False
                get_config.set(constants.MAVEN, constants.JDK, jdk_config[user_input - 1])
                config.save_config(get_config)
                print("selected jdk version: ", jdk_config[user_input - 1])
            else:
                print("Please select a valid jdk")
    else:
        print("JDK not config")
        sys.exit()


def get_jdk_path(get_config):
    if get_config is None:
        get_config = config.get_config()
    jsk_flag = get_config.get(constants.MAVEN, constants.JDK)
    if jsk_flag is None:
        set_jdk()
    return get_config.get(constants.JDK, jsk_flag)


def exc(command_line):
    get_config = config.get_config()
    path = get_jdk_path(get_config)

    # 添加ssl参数
    ssl = get_config.getboolean(constants.MAVEN, constants.SSL)
    if ssl is True:
        command_line = ssl_cmd(command_line)

    # 是否跳过test
    tests = get_config.getboolean(constants.MAVEN, constants.SKIP_TESTS)
    if tests is True:
        command_line = skip_tests(command_line)

    # 是否使用离线模式
    offline = get_config.getboolean(constants.MAVEN, constants.OFFLINE)
    if offline is True:
        command_line = offline_cmd(command_line)

    # 输入的其他参数
    args = sys.argv
    other_arg = ''
    if len(args) > 0:
        other_arg = ' '.join(str(element) for element in args[1:])

    # 执行的命令
    cmd = get_config.get(constants.MAVEN, constants.CMD)
    # 是否开启多线程
    multithread = get_config.getboolean(constants.MAVEN, constants.MULTITHREAD)
    if multithread is True:
        other_arg += " -T 1C"

    command_line = command_line.format(cmd)
    command_line += other_arg
    # 打印执行的命令
    print(command_line)
    final_command = f"set {constants.JAVA_HOME}={path}&& {command_line}"
    command.exe_command(final_command)
    # 是否执行flatten 插件的清除命令
    if "mcl" not in args[0]:
        flatten_clean = get_config.getboolean(constants.MAVEN, constants.FLATTEN_CLEAN)
        if flatten_clean is True:
            command.exe_command(constants.MAVEN_FLATTEN_CLEAN.format(cmd))
